package org.jeecg.jt808.operate;

import org.jeecg.jt808.JTT808Encoder;
import org.jeecg.jt808.JTT808Message;
import org.jeecg.jt808.Jt808Control.Jt808CentralControlConfig;
import org.jeecg.jt808.modbus.ModbusParams;
import org.jeecg.jt808.processor.JT808MessageGenerator;
import org.jeecg.jt808.vo.Packet;
import org.jeecg.jt808.processor.MessageType;
import static org.jeecg.jt808.util.JT808Util.bytesToHexString;

public class SetSpeedLimitMessageGenerator implements JT808MessageGenerator {

    @Override
    public String generateMessage(JTT808Message message) {
        return "";
    }

    @Override
    public String generateMessage(JTT808Message message, Jt808CentralControlConfig config) {
        message.packetCount = 0;
        message.packetIndex = 0;
        message.id = 0x8103; // 设置终端参数

        Packet p = Packet.create(0);
        if (config.getSpeedLimitEnabled() != null   ) {
            p = Packet.create(7); // 参数项长度
            p.addInt(0x0200); // 限速功能开关, 参数 ID
            p.addByte((byte) 1); // 参数长度: 1 字节
            p.addByte(config.getSpeedLimitEnabled() ? (byte) 1 : (byte) 0); // 参数值: 1 启用, 0 关闭
        }

        message.body = p.getBytes();

        // 在消息体头部添加参数总数
        byte[] body = p.getBytes();
        byte[] newBody = new byte[body.length + 1];
        newBody[0] = (byte) 1; //  参数总数
        System.arraycopy(body, 0, newBody, 1, body.length);
        message.body = newBody;
        return bytesToHexString(JTT808Encoder.encode(message));
    }

    @Override
    public String generateMessage(JTT808Message message, ModbusParams params) {
        return "";
    }

    @Override
    public String getType() {
        return MessageType.SET_SPEED_LIMIT.name(); //  新的消息类型
    }
}
